var qs = window.qs;
export default {
  fmtObject(obj = {}) {
    //  格式化JSON对象
    return JSON.parse(JSON.stringify(obj));
  },

  setItem(key, val) {
    //  格式化存储
    localStorage.setItem(
      key,
      typeof val == "string" ? val : JSON.stringify(val)
    );
  },

  makeUrl(url = location.href, cover) {
    //  重新拼装url  cover 去覆盖原地址参数
    var obj = qs.parse(url.split("?")[1]);
    return location.href.split("?")[0] + qs.stringify({}, obj, cover);
  },

  getMousePos(e = window.event) {
    //  获取鼠标坐标
    return { x: e.clientX, y: e.clientY };
  },

  keyCount(val = "") {
    //  获取对象键值数
    if (typeof val == "string") return val.length;
    return Object.getOwnPropertyNames(val).length;
  },
  deepClone(data) {
    var type = getObjType(data);
    var obj;
    if (type === "array") {
      obj = [];
    } else if (type === "object") {
      obj = {};
    } else {
      // 不再具有下一层次
      return data;
    }
    if (type === "array") {
      for (var i = 0, len = data.length; i < len; i++) {
        obj.push(deepClone(data[i]));
      }
    } else if (type === "object") {
      for (var key in data) {
        obj[key] = deepClone(data[key]);
      }
    }
    return obj;
  }
};
